<div class="box box-solid no-margin box-show">
    <div class="box-body">
        <div class="box-body table-responsive no-padding">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th><input type="checkbox" class="grid-select-all" />&nbsp;</th>
                    <th>批次号</th>
                    <th>箱号</th>
                    <th>FNSKU</th>
                    <th>账号</th>
                    <th>仓库</th>
                    <th>站点</th>
                    <th>SKU</th>
                    <th>箱数</th>
                    <th>单箱数量</th>
                    <th>数量</th>
                    <th>本次拆箱数</th>
                    <th>说明</th>
                </tr>
                </thead>
                <tbody>
                    @foreach($cis as $row)
                        @php
                            $isWarehouseDropShipping = \App\Models\OverSeasBa\Warehouse::isWarehouseDropShipping($row->warehouse_id);
                        @endphp
                        <tr>
                            <td>
                                @if($isWarehouseDropShipping['flag'])
                                    <input type="checkbox" class="grid-row-checkbox" data-id="{{ $row->id }}" />
                                @endif
                            </td>
                            <td>{{ $row->batch_no }}</td>
                            <td>{{ $row->box_no }}</td>
                            <td>
                                {{ $row->fnsku }}
                                <input type="hidden" name="fnsku" value="{{ $row->fnsku }}">
                            </td>
                            <td>
                                {{ \App\Models\Basics\SysPlatAccount::getAccountById($row->account_id) }}
                                <input type="hidden" name="account_id" value="{{ $row->account_id }}">
                            </td>
                            <td>
                                {{ $isWarehouseDropShipping['code'] }}
                                <input type="hidden" name="warehouse_id" value="{{ $row->warehouse_id }}">
                            </td>
                            <td>{{ \App\Models\Basics\SysArea::getSysAreaById($row->area_id) }}</td>
                            <td>
                                {{ $row->sku->psku_code ?? '' }}
                                <input type="hidden" name="psku_id" value="{{ $row->sku->id }}">
                            </td>
                            <td>{{ $row->storage_total - $row->out_total }}</td>
                            <td>{{ $row->num_per_box }}</td>
                            <td>{{ ($row->storage_total - $row->out_total) * $row->num_per_box }}</td>
                            <td>
                                @if($isWarehouseDropShipping['flag'])
                                    <input type="text" class="split_box_num_"
                                           style="width: 100px; text-align: right;"
                                           name="split_box_num"
                                           data-value="{{ max($row->storage_total - $row->out_total, 0) }}"
                                           value="{{ max($row->storage_total - $row->out_total, 0) }}">
                                @endif
                            </td>
                            <td>
                                @if(!$isWarehouseDropShipping['flag'])
                                该仓库无一件代发功能
                                @endif
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
<div style="margin-top: 10px;">
    <div class="box-body fields-group">
        <div class="form-group  ">
            <label class="col-sm-3  control-label"></label>
            <div class="col-sm-5">
                <label class="col-sm-5 control-label"></label>
                <button type="button" class="btn btn-primary" id="split-submit">提交</button>
            </div>
        </div>
    </div>
</div>

<script language="JavaScript" type="text/javascript">
    function unique(arr) {
        return Array.from(new Set(arr))
    }
    $(function () {
        $('.grid-row-checkbox').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {
            if (this.checked) {
                $(this).closest('tr').css('background-color', '#ffffd5');
            } else {
                $(this).closest('tr').css('background-color', '');
            }
        });
        $('.grid-select-all').iCheck({checkboxClass:'icheckbox_minimal-blue'});
        $('.grid-select-all').on('ifChanged', function(event) {
            if (this.checked) {
                $('.grid-row-checkbox').iCheck('check');
            } else {
                $('.grid-row-checkbox').iCheck('uncheck');
            }
        });

        let selectedRows = function () {
            let param = {};
            let selected = [];
            let detail = {};
            $('.grid-row-checkbox:checked').each(function(index){
                let id = $(this).data('id');
                selected.push(id);
                let indexArr = {};
                indexArr['id'] = id;
                let tr = $(this).closest('tr');
                indexArr['account_id'] = tr.find('input[name="account_id"]').val();
                indexArr['fnsku'] = tr.find('input[name="fnsku"]').val();
                indexArr['warehouse_id'] = tr.find('input[name="warehouse_id"]').val();
                indexArr['psku_id'] = tr.find('input[name="psku_id"]').val();
                indexArr['split_box_num'] = tr.find('input[name="split_box_num"]').val();
                detail[indexArr['id']] = indexArr;
            });
            param.selected = selected;
            param.detail = detail;
            return param;
        };

        $('#split-submit').on('click', function() {
            $(this).prop('disabled',true);
            let param = selectedRows();
            let ids = param.selected;
            if(ids.length == 0) {
                $(this).prop('disabled',false);
                swal('请选择拆箱单据', '', 'error');
                return false;
            }
            let detail = param.detail;
            $.ajax({
                method: 'post',
                url: '/xqc/overseas-ba/box-in-ajax',
                data: {
                    _token:"{{ csrf_token() }}",
                    param: { detail : detail },
                    action: 'split'
                },
                success: function (data) {
                    if (typeof data === 'object') {
                        if (data.status) {
                            $.pjax.reload('#tran-form');
                            toastr.success(data.message);
                        } else {
                            swal(data.message, '', 'error');
                        }
                    }
                }
            });
        });
        $(".split_box_num_").inputmask("integer",
            {
                onKeyValidation: function () {
                    let v = $(this).data('value');
                    let thisV = $(this).val();
                    if(thisV < 0 || thisV > v){
                        $(this).val(v);
                    }
                }
            }
        );
    });
</script>
